package com.mycompany.myapp.service;

import com.mycompany.myapp.domain.ElectricBicycle;
import com.mycompany.myapp.domain.Stewarder;
import com.mycompany.myapp.repository.ElectricBicycleRepository;
import com.mycompany.myapp.repository.StewarderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by wys on 2018/7/23
 */

@Service
public class StewarderService implements StewarderServiceInterface {

    @Autowired
    StewarderRepository stewarderRepository;
    ElectricBicycleRepository electricBicycleRepository;

    @Override
    public List<Stewarder> getAllSteward() {
        List<Stewarder> stewards = stewarderRepository
            .findAll();
        return stewards;
    }

    @Override
    public List<Stewarder> getAllFreeSteward() {

        List<Stewarder> stewards = stewarderRepository
            .findAllByStewardState("true");
        return stewards;
    }

    @Override
    public String maintenanceBicycleSituation(String bicycleID, String stewarderID, String stewardState) {

        String state = "";
        /**
         * false 维护成功  true没得
         */
        if(stewardState.equals("false")) {
            state = "steward_true";
        }else if(stewardState.equals("true")) {
            /**
             * 发放回去
             */
            ElectricBicycle electricBicycle = electricBicycleRepository
                .findElectricBicycleByElectricBicycleID(bicycleID);
            electricBicycle.setStewardState("false");
            electricBicycleRepository.save(electricBicycle);
            state = "steward_false";
        }else {
            state = "unknown";
        }
        return state;
    }

    @Override
    public String getStewarderForElecBicycle(String stewarderID) {

        String elecBicycleID;
        String state;

        Stewarder stewarder = stewarderRepository.findStewardByStewarderID(stewarderID);

        if(stewarder == null) {
            state = "no_this_steward";
            return state;
        }
        if (stewarder.getStewardBicycle()==null ||stewarder.getStewardBicycle().equals("")) {
            elecBicycleID = "nobicycle";
        }else {
            elecBicycleID = stewarder.getStewardBicycle();
        }

        return elecBicycleID;
    }
}
